<?php
/**
 * 名人机构管理
 * 
 * @author jiaowei <jiaowei@178.com>
 * @version $Id: star.php 6709 2010-05-20 10:04:21Z jiaowei $
 */
class friends_controller_admin_star extends AdminController
{
	/**
	 * 名人机构管理
	 *
	 */
	public function index_action()
	{
		$this->uid = _('uid', 0);
		//$this->username = _('username', '');
		$this->userInfo = Model::factory('friends_model_star')->getUser($this->uid);
		
		$allCate = Model::factory('friends_model_star')->getAllCate();
		$this->ret = $this->_getchilds($allCate);
		
		if (($_SERVER['REQUEST_METHOD'] == 'POST') && $_POST['add']) {
			$userInfo = $_POST['user'];
			$userInfo = array_map('trim', $userInfo);
			
			if (!$userInfo['cate']) exit('error : cate_name is empty');
			
			$update 			 = array();
			$update['type'] 	 = $userInfo['type'];
			$userInfo['parent_uid'] ? ($update['parent_uid'] = $userInfo['parent_uid']) : '';
			
			//处理分类
			if (false !== strpos($userInfo['cate'], '_')) {
				list($cate_1, $cate_2) = explode('_', $userInfo['cate']);
			} else {
				$cate_1 = $userInfo['cate'];
				$cate_2 = 0;
			}
			
			foreach ($this->ret as $k => $v) {
				if ($v['id'] == $cate_1) {
					$update['cate_1'] = $v['id'];
					$update['cate_1_name'] = $v['cate_name'];
				}
				if ($v['id'] == $cate_2) {
					$update['cate_2'] = $v['id'];
					$update['cate_2_name'] = $v['cate_name'];
				}
			}

			Model::factory('friends_model_star')->updateUser($update, $userInfo['uid']);
			
			//父类子类处理
			if ($userInfo['parent_uid']) {
				$parentUserInfo = Model::factory('friends_model_star')->getUser($userInfo['parent_uid']);//获取父级用户信息
				$childs = array();
				if ($parentUserInfo && $parentUserInfo['childs']) $childs = unserialize($parentUserInfo['childs']);
				if (!in_array($userInfo['uid'], $childs)) $childs[] = $userInfo['uid'];
				Model::factory('friends_model_star')->updateUser(array('childs' => serialize($childs)), $userInfo['parent_uid']);
			}
			
		}//end post
		
		
		$this->allStars = Model::factory('friends_model_star')->getStars(0, 0, true);
		
	}
	
	/**
	 * 名人机构分类管理
	 *
	 */
	public function cate_action()
	{
		$allCate = Model::factory('friends_model_star')->getAllCate();
		$cates = $this->_getchilds($allCate);
		
		//添加分类
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$cateInfo = $_POST['cate'];
			$cateInfo = array_map('trim', $cateInfo);
			if (!$cateInfo['cate_name']) exit('error : cate_name is empty');
			Model::factory('friends_model_star')->addCate($cateInfo);
			header('Location: ' . AdminController::_makeURL('friends','star','cate'));
		}
	
		$this->ret = $cates;
	}
	
	/**
	 * 修改分类
	 *
	 */
	public function update_cate_action()
	{
		$id = (int) _('id', 0);
		$allCate = Model::factory('friends_model_star')->getAllCate();
		$cates = $this->_getchilds($allCate);
		
		//添加分类
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$cateInfo = $_POST['cate'];
			$cateInfo = array_map('trim', $cateInfo);
			if (!$cateInfo['cate_name']) exit('error : cate_name is empty');
			Model::factory('friends_model_star')->editCate($cateInfo, $id);
			header('Location: ' . AdminController::_makeURL('friends','star','cate'));
		}
		
		foreach ($allCate as $k => $v) 
			if ($v['id'] == $id) $this->update = $v;

		$this->ret = $cates;
	}
	
	
	/**
	 * 名人机构分类递归
	 *
	 * @param array $cates
	 * @param int $id
	 * @param int $dep
	 * @return array
	 */
	private function _getchilds($cates, $id = 0, $dep = 0)
	{
		$dep++;
    	$tree = array(); 
    	foreach ($cates as $k => $v) {
    		if ($v['parent_id'] == $id) {
    			$v['dep'] = $dep - 1; $tree[] = $v;
    			if ($childs = $this->_getchilds($cates, $v['id'], $dep))
    				$tree = array_merge($tree, $childs);
        	}
    	}
    	return $tree; 
	}
	
}